using System;
using System.Windows.Forms;
using CSettingsUI;
using System.Diagnostics;
using System.ComponentModel;

namespace CSettingsUI
{
    [DefaultProperty("ValNew")]
    public class ButtonOption : Button, IPropertyConverterControl
	{
        private object val;
        public ButtonOption()
        {
            this.DataBindings.Clear();
            //this.OnClick += new System.EventHandler(OnClick);
			//this.OnClick
            //this.OnClick 
            
        }
       
        protected override void OnClick(EventArgs e)
        {
            if (val != null)
            {
                CSettingsUI.SettingForm sf = new CSettingsUI.SettingForm();
                sf.SelectedObject = this.val;
                sf.ShowDialog(this);
                this.val = sf.SelectedObject;
                base.Text = val.ToString();
                this.ValNew = val;

                //this.OnValidating(new System.ComponentModel.CancelEventArgs());
                //this.OnValidated(new EventArgs());

            }
            else
            {

            }
            //SaveSettings();

        }
       
        public new string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                Debug.WriteLine(base.Text);
                base.Text = value;

            }
        }
        public object ValNew
        {
            get
            {
                return this.val;

            }
            set
            {
                this.val = value;
            }
        }
        public object Value
        {
            get
            {
                // TODO:  Add TextBoxOption.Value getter implementation
                return val;
            }
            set
            {
                
               
                if (value != null)
                {
                    

                    Type t = value.GetType();
                    if (t.IsInterface || t.IsClass)
                    {
                        if(t != typeof(System.DBNull))
                        {
                            this.Text = "Click for changing the value!";
                            val = value;
                            this.ValNew = value;
                            return;
                        }

                    }
                }
                this.Text = "Value = null; Cannot change";


            }

        }
    }
    

}
